package com.talent.jiwen_teacher;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.talent.jiwen_teacher.base.BaseFragment;
import com.talent.jiwen_teacher.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.http.result.SmsResult;
import com.talent.jiwen_teacher.util.AMUtils;
import com.talent.jiwen_teacher.util.Constant;
import com.talent.jiwen_teacher.util.Validators;
import com.talent.jiwen_teacher.util.downtime.DownTimer;
import com.talent.jiwen_teacher.util.downtime.DownTimerListener;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import rx.Observable;

public class PhoneCodeLoginFragment extends BaseFragment {
    @BindView(R.id.phoneEt)
    EditText phoneEt;
    @BindView(R.id.getCodeBtn)
    Button getCodeBtn;
    @BindView(R.id.codeEt)
    EditText codeEt;

    private DownTimer downTimer;

    @Override
    protected void initData() {

        downTimer = new DownTimer();
        downTimer.setListener(new DownTimerListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                if(getCodeBtn != null){
                    getCodeBtn.setText(String.valueOf(millisUntilFinished / 1000) + "s");
                    getCodeBtn.setClickable(false);
                    getCodeBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_gray));
                }

            }

            @Override
            public void onFinish() {
                if (getCodeBtn != null){
                    getCodeBtn.setText(R.string.get_code);
                    getCodeBtn.setClickable(true);
                    getCodeBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_blue));
                }

            }
        });


        phoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 11) {
                    if (AMUtils.isMobile(s.toString().trim())) {
                        getCodeBtn.setClickable(true);
                        AMUtils.onInactive(getActivity(), phoneEt);
                        getCodeBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_blue));
                    } else {
                        Toast.makeText(getActivity(), R.string.Illegal_phone_number, Toast.LENGTH_SHORT).show();
                    }
                } else {
                    getCodeBtn.setClickable(false);
                    getCodeBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_gray));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        codeEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 4) {
                    AMUtils.onInactive(getActivity(), codeEt);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        getCodeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(phoneEt.getText().toString().trim())) {
                    showToast(getString(R.string.phone_number_is_null));
                } else {
                    downTimer.startDown(60 * 1000);
                    sendCode();
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != downTimer){
            downTimer.stopDown();
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_phone_code;
    }

    /**发送验证码
     *
     */
    private void sendCode(){
        Map<String, String> map = new HashMap<>();
        map.put("method", Constant.LOGIN_WITH_CODE);
        map.put("phone", phoneEt.getText().toString());

        Observable observable = ApiFactory.getEducationApiSingleton().sendMsg(map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<SmsResult>(getActivity()) {
            @Override
            protected void _onNext(SmsResult smsResult) {
//                if(smsResult!=null){
//                    String code=smsResult.getVcode();
//                    if(Validators.isEmpty(code)){
//                        showToast(getString(R.string.code_get_fill));
//                    }
//                }else{
//                    showToast(getString(R.string.data_is_wrong));
//                }
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);

    }

    public String getPhoneNumber(){
        return phoneEt.getText().toString();
    }

    public String getCode(){
        return codeEt.getText().toString();
    }
}
